<template>
  <el-dialog width="500px" :visible.sync="visible" top="17vh" title="添加区域">
    <el-form
      ref="predictFormRef"
      :model="predictForm"
      :rules="rules"
      label-width="140px"
    >
      <el-form-item label="区域类型">
        <el-select v-model="predictForm.area_type" placeholder="请选择区域类型">
          <el-option
            v-for="item of typeOption"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区域名称" prop="name">
        <el-input v-model="predictForm.name"></el-input>
      </el-form-item>

      <el-form-item label="区域坐标" prop="lon">
        <el-col :span="11">
          <el-form-item prop="lon">
            <el-input v-model="predictForm.lon" placeholder="经度"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11" :offset="2">
          <el-form-item prop="lat">
            <el-input v-model="predictForm.lat" placeholder="维度"></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="区域大小（米）" prop="scale">
        <el-input
          type="number"
          v-model="predictForm.scale"
          placeholder="区域大小（米）"
          min="0"
        ></el-input>
      </el-form-item>
      <el-form-item label="区域热点值" prop="value">
        <el-input
          type="number"
          v-model="predictForm.value"
          placeholder="区域热点值"
          min="0"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="predictSave()" :loading="loading"
        >添加</el-button
      >
    </div>
  </el-dialog>
</template>
  
  <script>
import * as request from "../http/request.js";
// 默认表单数据
const baseForm = {
  area_type: "演习", //区域类型
  name: "", //区域名称
  lon: null, // 起点经度
  lat: null, // 纬度
  scale: null, //大小
  value: 1, //热点值
};
const validateLongitude = (rule, value, callback) => {
  //经度,整数部分为0-180小数部分为0到15位
  var longreg =
    /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,15})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,15}|180)$/;
  if (!longreg.test(value)) {
    // callback(new Error("经度整数部分为0-180,小数部分为0到15位!"));
    callback(new Error("经度格式错误!"));
  }
  callback();
};
const validateLatitude = (rule, value, callback) => {
  //纬度,整数部分为0-90小数部分为0到15位
  var latreg = /^(\-|\+)?([0-8]?\d{1}\.\d{0,15}|90\.0{0,15}|[0-8]?\d{1}|90)$/;
  if (!latreg.test(value)) {
    // callback(new Error("纬度整数部分为0-90,小数部分为0到15位!"));
    callback(new Error("纬度格式错误!"));
  }
  callback();
};

export default {
  data() {
    return {
      predictForm: { ...baseForm },
      visible: false,
      loading: false,
      rules: {
        lon: [
          { required: true, message: "请填写当前经度", trigger: "blur" },
          { validator: validateLongitude, trigger: "blur" },
        ],
        lat: [
          { required: true, message: "请填写当前维度", trigger: "blur" },
          { validator: validateLatitude, trigger: "blur" },
        ],

        area_type: [{ required: true, message: "请选择区域", trigger: "change" }],
        name: [{ required: true, message: "请填写区域名称", trigger: "blur" }],
        scale: [
          { required: true, message: "请填写区域大小（米）", trigger: "blur" },
        ],
        value: [{ required: true, message: "", trigger: "blur" }],
      },
      typeOption: [
        {
          name: "演习",
          value: "演习",
        },
        {
          name: "挑衅",
          value: "挑衅",
        },
       
         {
          name: "补给",
          value: "补给",
        },
         {
          name: "基地",
          value: "基地",
        },
         {
          name: "热点类型",
          value: "热点类型",
        }
      ],
    };
  },
  methods: {
    // 初始化方法
    show() {
      // 显示
      this.visible = true;
      this.loading = false;
      // 重置表单
      this.predictForm = { ...baseForm };
    },
    // 转换数据类型
    toNumberByFiled(data, filed) {
      if (data[filed]) {
        return +data[filed];
      }
      return null;
    },
    // 保存
    predictSave() {
      this.$refs.predictFormRef.validate((valid) => {
        if (valid) {
          const data = { ...this.predictForm };
          //  如果有数据，就转成数字类型
          data.lon = this.toNumberByFiled(data, "lon");
          data.lat = this.toNumberByFiled(data, "lat");
          this.loading = true;

          request
            .areaAdd_area(data)
            .then((res) => {
              const data = res.data;
              console.log(res);
              if (data.status === "1") {
                this.$message.success(data.message);
                this.visible = false;
                this.$emit("success");
              }
              if (data.status === "-1") {
                this.$message.error(data.message);
              }
            })
            .finally(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
  
  <style>
</style>